﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace LayoutInspection
{
    class Utility
    {
        private string[] strWkspsPath = new string[51];

        public void CollectWkspsPath(string strRootFolder)
        {
            //string[] strWkspFiles = 
            //    Directory.GetFiles(strRootFolder, "*.sxw", SearchOption.TopDirectoryOnly);
            //if (strWkspFiles == null || strWkspFiles.Length == 0)
            //{
                string[] strWkspFolders =
                    Directory.GetDirectories(strRootFolder, "*_整理", SearchOption.TopDirectoryOnly);
                if (strWkspFolders != null && strWkspFolders.Length > 0)
                {
                    int idx = 0;
                    foreach (string strDirPath in strWkspFolders)
                    {
                        string[] strFilesPath =
                            Directory.GetFiles(strDirPath, "*.sxw", SearchOption.TopDirectoryOnly);
                        if (strFilesPath != null && strFilesPath.Length == 1)
                        {
                            strWkspsPath[idx++] = strFilesPath[0];
                            //strWkspsPath[idx++] = String.Format(
                            //    @"{0}\{1}\{2}", strRootFolder, strDirPath, strFilesPath[0]);
                        }
                    }
                }
                
            //}
        }

        public void ConnectLayoutToWksp(
            AxSuperMapLib.AxSuperWorkspace superWksp, AxSuperLayoutLib.AxSuperLayout superLayout)
        {
            //superLayout.Connect(superWksp.CtlHandle);
            //LayoutMgmt layoutMgmt = new LayoutMgmt(superLayout);

            foreach (string strWkspPath in strWkspsPath)
            {
                if (strWkspPath != null)
                {
                    if (superWksp.Open(strWkspPath))
                    {
                        soLayouts objLayouts = superWksp.Layouts;

                        superLayout.Connect(superWksp.CtlHandle);

                        LayoutMgmt layoutMgmt = new LayoutMgmt(superLayout);
                        Console.WriteLine("layout count:" + objLayouts.Count.ToString());
                        if (layoutMgmt.PrintLayoutAsPDF(objLayouts))
                        {
                            Console.WriteLine("Output successfully!");
                        }

                        superLayout.Elements.RemoveAll();
                        Marshal.ReleaseComObject(objLayouts);
                        objLayouts = null;

                        superLayout.Disconnect();
                        superWksp.Close();
                    }
                    else
                    {
                        throw new Exception("Workspace open failed!");
                    }
                }
            }

            //superLayout.Disconnect();
            //superWksp.Close();
        }
    }
}
